package helper.com.listener;

import helper.com.cache.CacheOps;
import helper.com.domain.Manager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import static helper.com.constants.CacheConstants.MANAGER_KEY_PREFIX;

/**
 * @author 大忽悠
 * @create 2022/5/20 15:03
 */
@CanalTable("manager")
@Component
@Slf4j
@RequiredArgsConstructor
public class CanalManagerListener implements EntryHandler<Manager> {
    private final CacheOps cacheOps;
    @Override
    public void insert(Manager manager) {
        cacheOps.saveOne(MANAGER_KEY_PREFIX+manager.getAccount(),manager);
        log.info("manager表插入了一条记录,{}",manager);
    }

    @Override
    public void update(Manager before, Manager after) {
        cacheOps.delOne(MANAGER_KEY_PREFIX+before.getAccount());
        cacheOps.saveOne(MANAGER_KEY_PREFIX+after.getAccount(),after);
        log.info("manager表更新了一条记录,更新前: {}, 更新后: {}",before,after);
    }

    @Override
    public void delete(Manager manager) {
        cacheOps.delOne(MANAGER_KEY_PREFIX+manager.getAccount());
        log.info("manager表删除了一条记录,该记录为: {}",manager);
    }
}
